package day230831;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/8/31 19:54
 */
public class Solution01 {

    public int lengthOfLongestSubstring(String s) {
        char[] charArray = s.toCharArray();
        Set<Character> set = new HashSet<>(); // 窗口
        int start = 0, end = 0; // 窗口的起止位置
        int maxLen = 0; // 记录最长不重复子串的长度
        while (start < charArray.length && end < charArray.length) {
            if (set.contains(charArray[end])) {
                set.remove(charArray[start]);
                ++start;
                maxLen = Math.max(end - start + 1, maxLen);
            } else {
                set.add(charArray[end]);
                maxLen = Math.max(end - start + 1, maxLen);
                ++end;
            }
        }
        return maxLen;
    }
}
